/*     */ package com.zimbra.cs.service.mail;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.mailbox.MailItem;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.Mountpoint;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import com.zimbra.cs.service.util.ItemIdFormatter;
/*     */ import com.zimbra.soap.DocumentHandler;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.util.Map;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public abstract class MailDocumentHandler
/*     */   extends DocumentHandler
/*     */ {
/*     */   protected String[] getProxiedIdPath(Element request)
/*     */   {
/*  38 */     return null;
/*     */   }
/*     */   
/*     */   protected boolean checkMountpointProxy(Element request) {
/*  42 */     return false;
/*     */   }
/*     */   
/*     */   protected String[] getResponseItemPath() {
/*  46 */     return null;
/*     */   }
/*     */   
/*     */   protected Element proxyIfNecessary(Element request, Map<String, Object> context)
/*     */     throws ServiceException
/*     */   {
/*  52 */     String[] xpath = getProxiedIdPath(request);
/*  53 */     String id = xpath != null ? getXPath(request, xpath) : null;
/*     */     
/*  55 */     if (id != null) {
/*  56 */       ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  57 */       OperationContext octxt = getOperationContext(zsc, context);
/*  58 */       ItemId iid = new ItemId(id, zsc);
/*     */       
/*     */ 
/*  61 */       ItemId iidTarget = getProxyTarget(zsc, octxt, iid, checkMountpointProxy(request));
/*  62 */       if (iidTarget != null) {
/*  63 */         return proxyRequest(request, context, iid, iidTarget);
/*     */       }
/*     */     }
/*     */     
/*  67 */     return super.proxyIfNecessary(request, context);
/*     */   }
/*     */   
/*     */   protected static ItemId getProxyTarget(ZimbraSoapContext zsc, OperationContext octxt, ItemId iid, boolean checkMountpoint) throws ServiceException {
/*  71 */     if ((zsc == null) || (iid == null)) {
/*  72 */       return null;
/*     */     }
/*  74 */     Account acct = getRequestedAccount(zsc);
/*  75 */     if (!iid.belongsTo(acct)) {
/*  76 */       return iid;
/*     */     }
/*     */     
/*  79 */     if ((!checkMountpoint) || (!Provisioning.onLocalServer(acct))) {
/*  80 */       return null;
/*     */     }
/*  82 */     Mailbox mbox = getRequestedMailbox(zsc);
/*  83 */     MailItem item = mbox.getItemById(octxt, iid.getId(), MailItem.Type.UNKNOWN);
/*  84 */     if (!(item instanceof Mountpoint)) {
/*  85 */       return null;
/*     */     }
/*  87 */     return ((Mountpoint)item).getTarget();
/*     */   }
/*     */   
/*     */   private void insertMountpointReferences(Element response, String[] xpath, ItemId iidMountpoint, ItemId iidLocal, ZimbraSoapContext lc) {
/*  91 */     int depth = 0;
/*  92 */     while ((depth < xpath.length) && (response != null)) {
/*  93 */       response = response.getOptionalElement(xpath[(depth++)]);
/*     */     }
/*  95 */     if (response == null) {
/*  96 */       return;
/*     */     }
/*     */     
/*  99 */     ItemIdFormatter ifmt = new ItemIdFormatter(lc);
/* 100 */     String local = iidLocal.toString(ifmt);
/* 101 */     for (Element elt : response.listElements()) {
/* 102 */       String folder = elt.getAttribute("l", null);
/* 103 */       if (local.equalsIgnoreCase(folder)) {
/* 104 */         elt.addAttribute("l", iidMountpoint.toString(ifmt));
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   protected Element proxyRequest(Element request, Map<String, Object> context, ItemId iidRequested, ItemId iidResolved)
/*     */     throws ServiceException
/*     */   {
/* 112 */     boolean mountpoint = iidRequested != iidResolved;
/* 113 */     if (mountpoint) {
/* 114 */       setXPath(request, getProxiedIdPath(request), iidResolved.toString());
/*     */     }
/*     */     
/* 117 */     Element response = proxyRequest(request, context, iidResolved.getAccountId(), mountpoint);
/*     */     
/*     */ 
/* 120 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/* 121 */     String[] xpathResponse = getResponseItemPath();
/* 122 */     if ((mountpoint) && (xpathResponse != null))
/* 123 */       insertMountpointReferences(response, xpathResponse, iidRequested, iidResolved, zsc);
/* 124 */     return response;
/*     */   }
/*     */   
/*     */   private Element proxyRequest(Element request, Map<String, Object> context, String acctId, boolean mountpoint) throws ServiceException
/*     */   {
/* 129 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*     */     
/* 131 */     ZimbraSoapContext zscTarget = new ZimbraSoapContext(zsc, acctId);
/* 132 */     if (mountpoint) {
/* 133 */       zscTarget.recordMountpointTraversal();
/*     */     }
/*     */     
/* 136 */     return proxyRequest(request, context, getServer(acctId), zscTarget);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/mail/MailDocumentHandler.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */